<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace console\controllers;

use Yii;
use yii\console\Controller;
use yii\helpers\Console;
use wx\models\Wechat;

use wx\sdk\MpWechat;

use wxpay\models\lib\NativePay;
use wxpay\models\lib\WxPayApi;
use wxpay\models\lib\WxPayConfig;
use wxpay\models\lib\WxPayNotify;
use wxpay\models\lib\WxPayUnifiedOrder;
use wxpay\models\lib\WxPayException;
use wxpay\models\lib\WxPayJsApiPay;

use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\components\ActionTimeFilter;

/**
 * This command echoes the first argument that you have entered.
 *
 * This command is provided as an example for you to learn how to create console commands.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class TestController extends Controller
{

    public $a;

    public $b;    

    public function options($actionID)
    {
        if ($actionID == 'demo') {
            return ['a'];
        }
        return ['a', 'b'];        
    }
    
    public function behaviors()
    {
        return [
            'actionTimeFilter' => [
                'class' => ActionTimeFilter::className(),
            ],
            
        ];
    }

    public function init()
    {        
        Yii::$app->getUrlManager()->setBaseUrl('/wx/web/index.php');
        Yii::$app->getUrlManager()->setHostInfo('http://wosotech.com');
        Yii::$app->getUrlManager()->setScriptUrl('/wx/web/index.php');
    }

    /**
     * This command echoes what you have entered as the message.
     * @param string $message the message to be echoed.
     * yii test/send-sm hello
     */
    public function actionSendSm($mobiles, $message = 'hello')
    {
        try {
            yii::$app->sm->send('15527210477', $message);
            echo yii::$app->sm->getBalance();
        } catch(\Exception $e) {
            die($e->getMessage());
        }
    }

    /*
    D:\htdocs\zq>yii test/demo 100 200 --a=1
    par1=100, par2=200, par3=3, a=1
    */
    public function actionDemo($par1, $par2, $par3=3)
    {
        $this->stdout("par1=$par1, par2=$par2, par3=$par3, a={$this->a}\n");
        return 0;
    }

    public function actionIndex()
    {
        yii::error(yii::getAlias('@app'));
    }
    
    public function actionProgress()
    {    
        Console::startProgress(0, 1000, 'Counting objects: ', false);
        for ($n = 1; $n <= 1000; $n++) {
            usleep(1000);
            Console::updateProgress($n, 1000);
        }
        Console::endProgress("done." . PHP_EOL);
        return;

        Console::startProgress(0, 1000);
        for ($n = 1; $n <= 1000; $n++) {
            usleep(1000);
            Console::updateProgress($n, 1000);
        }
        Console::endProgress();
    
    }
    
    public function actionEnter()
    {
        $this->stdout("Demo\n");
        $this->stderr("for CLI.\n");

        Console::output('AAAAAAA');
        Console::output('BBBBBBB');  
         flush();
        Console::error('CCCCC');
        Console::error('DDDDD');        
        $userId = $this->prompt('User ID:', ['default'=>'1234567890', 'pattern'=>'/^[0-9]+$/']);
        Console::output($userId);
        $true = $this->confirm('are you a boy?', true);
        if ($true) {
            Console::output('Yes');
        } else {
            Console::output('No');        
        }
        $select = $this->select('Select a color', ['red'=>'RED COLOR', 'green'=>'GREEN COLOR']);
        Console::output($select);                

    }
    
    //C:\xampp\php\php.exe d:\htdocs\zq\yii test/mail
    // d:\htdocs\zq>yii test/mail
    public function actionMail()
    {
/*    
        Yii::$app->mailer->compose('contact/html', ['contactForm' => $form])
            ->setFrom('from@domain.com')
            ->setTo($form->email)
            ->setSubject($form->subject)
            ->send();        
*/            

        $username = 'myname-hehbhehb';
        $password_reset_token = 'my-password_reset_token';
        $user = new \yii\base\DynamicModel(compact('username', 'password_reset_token'));
        
        Yii::$app->mailer->compose('passwordResetToken-html', ['user' => $user, 'resetLink' => 'http://baidu.com'])                    
            ->setFrom('romeo2004@sina.com')
            ->setTo('hehbhehb@sina.com')
            ->setSubject('hello, jack')
            ->send();        
        
    }
    
    public function actionWechat()
    {
        $ch = curl_init();
        //设置超时
        //marked by hehb begin
        //Log::DEBUG('3333.1');
        //curl_setopt($ch, CURLOP_TIMEOUT, $this->curl_timeout);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        //end
        curl_setopt($ch, CURLOPT_URL, "http://baidu.com");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,FALSE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $res = curl_exec($ch);
        var_dump($res);
        exit;

        $wechat = Wechat::findOne(1);
        var_dump($wechat->attributes);
        $mpWechat = $wechat->getMpWechat();
        var_dump($mpWechat->getAccessToken(true));
    }

    public function actionUser()
    {
        $user = \backend\models\User::findOne(1);
        var_dump($user->ext2);
        $user->ext1 = 'EXT1';
        $user->save();
        var_dump($user->attributes);
    }


}
